#include "advertising.h"
#include "ui_advertising.h"



advertising::advertising(QWidget *parent) :
    QMainWindow(parent),
    timer(new QTimer(this)),
    currentIndex(0),
    timer_clock(new QTimer(this)),
    ui(new Ui::advertising)
{
    ui->setupUi(this);
    //这样可以让按钮背景没有光标（即没有边框）
    ui->click_to_buy->setFocusPolicy(Qt::NoFocus);

    //readall directort
        direct();
       // 显示第一张图片
       showImage(0);
       //---------------------------time clock
       // 启动定时器，每秒触发一次
       connect(timer_clock, &QTimer::timeout, this, &advertising::updateTime);
       timer_clock->start(1000); // 1000毫秒 = 1秒
       // 初始显示时间
       updateTime();


}





// 显示指定索引的图片
void advertising::showImage(int index) {
    if (index >= 0 && index < imagePaths.size()) {
        //qDebug()<<imagePaths;
        QPixmap pixmap(imagePaths[index]);
        if (!pixmap.isNull()) {
            ui->photo_label->setPixmap(pixmap.scaled(ui->photo_label->size(), Qt::KeepAspectRatio));
        } else {
            qDebug() << "Failed to load pixmap from:" << imagePaths[index];
        }
        ui->photo_label->setPixmap(pixmap.scaled(ui->photo_label->size(), Qt::KeepAspectRatio)); // 保持图片比例缩放
    }
    ui->equipment_num->setText(equipment_num_label);
}

// 切换并显示下一张图片
void advertising::showNextImage() {
    currentIndex = (currentIndex + 1) % imagePaths.size(); // 实现循环播放
    showImage(currentIndex);
}


void advertising::updateTime()
{
    QTime currentTime = QTime::currentTime();
    QString timeText = currentTime.toString("hh:mm:ss");
    //qDebug() << currentTime.toString("hh:mm:ss t");
    ui->time_clock->setText(timeText);
}


void advertising::direct()
{
    //-------------------------文件操作
    QDir dir("/mnt/hgfs/aic_jeff/QT/项目文件夹/自动售卖机/client/image_advertising");
    if (!dir.exists()) {
        qDebug() << "Directory does not exist!";
        return;
    }

    QStringList filters;
    filters << "*.png" << "*.jpg"; // 设置文件过滤器
    QFileInfoList fileInfoList = dir.entryInfoList(filters, QDir::Files | QDir::NoDotAndDotDot); // 获取文件信息列表，排除"."和".."

    for (const QFileInfo &fileInfo : fileInfoList) {
        imagePaths << fileInfo.absoluteFilePath(); // 将文件的绝对路径添加到列表中
    }
    //qDebug()<<imagePaths;
    // 现在imagePaths包含了所有匹配文件的路径，您可以使用它来进行图片播放

    //ui->photo_label->setAlignment(Qt::AlignCenter); // 设置图片居中显示

        timer = new QTimer(this);
        connect(timer, &QTimer::timeout, this, &advertising::showNextImage); // 连接timer的timeout信号到showNextImage槽函数


        // 启动timer，比如每2秒切换一次图片
       timer->start(2000);

}


advertising::~advertising()
{
    delete ui;
}




